<?php
/*
 * author: P.L.A
 * create: 2017-08-23
 * */


// 应用公共文件
use think\Db;


/*获取并返回系统信息*/
 function systemInfo(){
	    $systemInfo = array(
			'服务器计算机名' => php_uname(),
			'服务器域名' => $_SERVER['SERVER_NAME'],
			'服务器IP' => gethostbyname($_SERVER['SERVER_NAME']),
            'WEB服务端口' => $_SERVER['SERVER_PORT'],
            '操作系统' => PHP_OS,
            '运行环境' => $_SERVER["SERVER_SOFTWARE"],
            '主机名' => $_SERVER['SERVER_NAME'],
            'PHP版本'=>PHP_VERSION,
            'ThinkPHP版本' => THINK_VERSION,
            '服务器语言' => $_SERVER['HTTP_ACCEPT_LANGUAGE'],
            '网站文档目录' => $_SERVER["DOCUMENT_ROOT"],
            '浏览器信息' => substr($_SERVER['HTTP_USER_AGENT'], 0, 40),
            '通信协议' => $_SERVER['SERVER_PROTOCOL'],
            '请求方法' => $_SERVER['REQUEST_METHOD'],
            '上传附件限制' => ini_get('upload_max_filesize'),
            '执行时间限制' => ini_get('max_execution_time') . '秒',
            '服务器时间' => date("Y年n月j日 H:i:s"),
            '北京时间' => gmdate("Y年n月j日 H:i:s", time() + 8 * 3600),
            '用户的IP地址' => $_SERVER['REMOTE_ADDR'],
			'Apache版本' => apache_get_version(),
			'ZEND版本' => zend_version(),
			'脚本运行占用最大内存' => get_cfg_var("memory_limit"),
			'当前进程用户名' => Get_Current_User(),
			'当前客户端IP' => $_SERVER['REMOTE_ADDR'],                                     
		);
        return $systemInfo;
    }

/*上传方法*/
function upload(){
    // 获取表单上传文件 例如上传了001.jpg
    $file = request()->file('img');
    // 验证文件信息并移动到框架应用根目录/uploads/ 目录下
    $info = $file->validate(['size'=>3145728,'ext'=>'jpg,png,gif,bmp'])->move(ROOT_PATH . 'public/upload');
    if($info){
        // 成功上传后 获取上传信息
        return $info->getSaveName();
    }else{
        // 上传失败获取错误信息
        echo $file->getError();
    }

}


/*获取用户全部权限*/
function getUserPerm($username){
    $user_perm_temp = Db::name('user')
        ->alias('a')
        ->join('am_user_perm b', 'a.id =b.u_id')
        ->field('b.perm_id,a.id,a.username,a.super_administrator,a.login_count')
        ->where('username' , $username)
        ->find();
    $user_perm = explode(',','$user_perm_temp');
    return $user_perm;
}


/*获取用户IP地址*/
function getIP(){
    global $ip;
    if (getenv("HTTP_CLIENT_IP"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if(getenv("HTTP_X_FORWARDED_FOR"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if(getenv("REMOTE_ADDR"))
        $ip = getenv("REMOTE_ADDR");
    else $ip = "Unknow IP";
    return $ip;
}


/*删除图片及空文件夹*/
function delDirAndFile($dir)
{
    $prefix = './public/upload/';
    //$foo = '20170830/4a4f139eab40cd82c70cea2e77713cf5.png';   /*这句只是用来测试substr()函数的*/
    $file_name = substr($dir,9);
    $dir = substr($dir,0,8);
    $dir = $prefix . $dir;
    $dh = opendir($dir);
    $res = '';/*删除文件及目录*/
    while ($file_name = readdir($dh))
    {
        unlink($file_name);
        if( readdir($dh) == ''){
            $res = rmdir($dir);
        }
    }
    if( $res ){
        return true;
    }else{
        return false;
    }

}


